<?php
/**
 * Created by PhpStorm.
 * User: 悟能科技
 * Date: 2018/7/2
 * Time: 9:54
 */

namespace app\agent\controller;


use app\common\model\Merchant;
use app\common\model\PaymentAd;
use think\Controller;
use think\Request;
use think\Session;

class Marke extends Controller
{

    //营销工具
    public function tools(){

        return $this->fetch();
    }

    //支付后广告
    public function advertlist(){
        $PaymentAd = new PaymentAd();
        $map['agent_id'] = Session::get('agentloginid');
        $list = $PaymentAd->where($map)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    //支付后广告添加
    public function advertadd(){
        if (Request()->isAjax()){
            $param = request()->post();
            $data = $param;
            $times=explode(" - ",$param['times']);
            $data['start_time']=strtotime($times[0]);
            $data['end_time']=strtotime($times[1]);
            $data['agent_id'] = Session::get('agentloginid');
            if (isset($param['mid'])){
                $data['put_in_mid'] = implode(',',$param['mid']);
            }
            $PaymentAd = new PaymentAd();
            $res = $PaymentAd->allowField(true)->save($data);
            if ($res){
                $rarr['code'] = 0;
                $rarr['message'] = 'success';
            }else{
                $rarr['code'] = 9001;
                $rarr['message'] = 'error';
            }
            return json($rarr);
        }else{

            return $this->fetch();
        }
    }

    //支付后广告修改
    public function advertedit(){
        $PaymentAd = new PaymentAd();
        $map['id'] = request()->param('id');
        $map['agent_id'] = Session::get('agentloginid');
        $info = $PaymentAd->where($map)->find();
        if ($info) {
            if (Request()->isAjax()) {
                $param = request()->post();
                $data = $param;
                $times = explode(" - ", $param['times']);
                $data['start_time'] = strtotime($times[0]);
                $data['end_time'] = strtotime($times[1]);
                if (isset($param['mid'])){
                    $data['put_in_mid'] = implode(',',$param['mid']);
                }
                $res = $PaymentAd->allowField(true)->save($data, $map);
                if ($res) {
                    $rarr['code'] = 0;
                    $rarr['message'] = 'success';
                } else {
                    $rarr['code'] = 9001;
                    $rarr['message'] = 'error';
                }
                return json($rarr);
            } else {
                $Merchant = new Merchant();
                $mach = $Merchant->where(['id'=>['in',$info['put_in_mid']]])->select();
                $this->assign('mach',$mach);
                $this->assign('info', $info);
                return $this->fetch();
            }
        }else{
            $this->error('您没有修改该条广告的权限');
        }
    }

    //支付后广告删除
    public function advertdel(){
        $PaymentAd = new PaymentAd();
        $map['id'] = request()->param('id');
        $map['agent_id'] = Session::get('agentloginid');
        $info = $PaymentAd->where($map)->find();
        if ($info) {
                $res = $PaymentAd->where($map)->delete();
                if ($res) {
                    $this->success('删除成功');
                } else {
                    $this->error('删除失败');
                }
        }else{
            $this->error('您没有修改该条广告的权限');
        }
    }

    //支付后广告状态
    public function advertstatu(){
        $PaymentAd = new PaymentAd();
        $map['id'] = request()->param('id');
        $map['agent_id'] = Session::get('agentloginid');
        $info = $PaymentAd->where($map)->find();
        if ($info) {
            $data['statu'] = $info['statu'] == 1?0:1;
            $res = $PaymentAd->save($data,$map);
            if ($res) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }else{
            $this->error('您没有修改该条广告的权限');
        }
    }


    //获取投放门店
    public function getputmach(){
        if (Request()->isAjax()){

            $Merchant = new Merchant();
            $map['agent'] = Session::get('agentloginid');
            $param = request()->post();

            if ($param['leibie'] !== '0'){
                $map['leibie'] = $param['leibie'];
            }
            if ($param['leibie2'] !== '0'){
                $map['leibie2'] = $param['leibie2'];
            }
            $list = $Merchant->where($map)->field('merchant,top_mid,id')->select();
//            return json($Merchant->getLastSql());
            if ($list){
                $data['code'] = 0;
                $data['message'] = 'SUCCESS';
                $data['data'] = $list;
            }else{
                $data['code'] = 0;
                $data['message'] = 'ERROR';
                $data['data'] = $list;
            }
            return json($data);
        }
    }

}